package com.apes.fn.ssm.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * @ClassName : StockConfig
 * @Description : 备货配置
 * @Author : WSl
 * @Date: 2022-6-21 10:37
 */

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = StockConfig.CONST_TABLE_NAME, indexes = {@Index(columnList = "location_id,fitting_message_id", unique = true)})
public class StockConfig extends Model {

    public final static String CONST_TABLE_NAME = "mas_stock";

    @Id
    @Column(name = "id", length = 10)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "备货配置")
    @Comment("编码")
    private String id;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "location_id", referencedColumnName = "id")
    @Comment("仓库")
    private Location location;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fitting_message_id", referencedColumnName = "id", nullable = false)
    @Comment("组合配件名称")
    private FittingMessage fittingName;

    @Column(name = "SKU_COVER", length = 20)
    @Comment("SKU覆盖率")
    private double SKUCover;

    @Column(name = "SKU_MAX", length = 20)
    @Comment("SKU上限")
    private int SKUMax;

    @Column(name = "SKU_MIN", length = 20)
    @Comment("SKU下限")
    private int SKUMin;

    @Column(name = "QTY_MAX", length = 20)
    @Comment("数量上限")
    private int QTYMax;

    @Column(name = "QTY_MIN", length = 20)
    @Comment("数量下限")
    private int QTYMin;

    @Column(name = "QTY_GRADED", length = 20)
    @Comment("数量梯度")
    private int qtyGraded;

    @Column(name = "VIP_QTY", length = 20)
    @Comment("会员基数")
    private int vipQty;

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("有效")//备货配置标记
    private boolean valid = true;

    //改写set方法数量不能为负数
    public void setSKUMax(int SKUMax) {
        this.SKUMax = Math.abs(SKUMax);
    }

    public void setSKUMin(int SKUMin) {
        this.SKUMin = Math.abs(SKUMin);
    }

    public void setQTYMax(int QTYMax) {
        this.QTYMax = Math.abs(QTYMax);
    }

    public void setQTYMin(int QTYMin) {
        this.QTYMin = Math.abs(QTYMin);
    }

    public void setQtyGraded(int qtyGraded) {
        this.qtyGraded = Math.abs(qtyGraded);
    }

    public void setVipQty(int vipQty) {
        this.vipQty = Math.abs(vipQty);
    }
}
